<template>
    <main>
        <article>
            <ul v-for="(item, index) of generateData">
                <strong>{{item.name}}</strong>
                <li 
                    v-for="(value, i) of item.child" 
                    @click="getSingerList(index, i, value.area, value.type)" 
                    :class="value.active"
                >{{value.title}}</li>
            </ul>
        </article>
        <aside>
            <header>华语男歌手</header>
            <nav>
                <div 
                    v-for="(item, index) of A_Z" 
                    :class="item.active"
                    @click="initial(index, item.name)"
                >{{ item.name }}</div>
            </nav>
            <Loading v-if="!singerData"></Loading>
            <ul v-else>
                <li 
                    v-for="item of singerData"
                    @click="toSingerRoute(item.id)"
                >
                    <img :src="item.picUrl" >
                    <span>{{item.name}}</span>
                </li>
            </ul>
        </aside>
    </main>
    <UserFooter></UserFooter>
</template>

<script setup>
    import { ref, getCurrentInstance, computed, onUpdated} from 'vue';
    import { useRouter } from 'vue-router';
    const axios = getCurrentInstance().proxy.$http;
    const router = useRouter();

    let singerData = ref();
    // 侧边导航兰
    const generateData = computed(() => {
        const data = ref([]);
        const arr = [{name: '华语', area: 7}, {name: '欧美', area: 96}, {name: '日本', area: 8}, {name: '韩国', area: 16}, {name: '其他', area: 0}]; 
        arr.forEach((value, index) => {
            data.value.push({
                name: value.name,
                child: []
            })
            for(let i = 1; i <= 3; i ++) {
                let title;
                if(i === 1) {
                    title = value.name + '男歌手'
                } else if(i === 2) {
                    title = value.name + '女歌手'
                } else {
                    title = value.name + '组合/乐队'
                }
                data.value[index].child.push({
                    type: i,
                    title,
                    area: value.area,
                    active: ''
                })
            }
        })
        return data.value
    }) 
    let oldActive = { index: 0, i: 0 }
    // 设置导航栏激活样式
    const navActiveStyle = (index, i) => {
        generateData.value[oldActive.index].child[oldActive.i].active = ''
        oldActive.index = index;
        oldActive.i = i;
        generateData.value[index].child[i].active = 'active'
    }
    // 获取歌手列表
    let artist = ref({
        area: 7,
        type: 1
    })
    const getSingerList = async (index, i, area, type) => {
        artist.area = area;
        artist.type = type;
        navActiveStyle(index, i);
        singerData.value = '';
        let { data } = await axios.get(`/artist/list?type=${type}&area=${area}`);
        singerData.value = data.artists;
        setTimeout(() => { document.documentElement.scrollTop = 0 })
        A_Z_style(0);
        // console.log(singerData.value);
    }
    getSingerList(0, 0, 7, 1)



    // 内容区A-Z字符导航栏
    const A_Z = computed(() => {
        let arr = ref([{name: '热门', active: 'active'}]);
        for(let i = 65; i < 91; i ++) {
            arr.value.push({name: String.fromCharCode(i), active: ''});
        }
        return arr.value
    })
    
    let oldIndex = 0;
    const A_Z_style = (index) => {
        A_Z.value[oldIndex].active = '';
        oldIndex = index;
        A_Z.value[index].active = 'active';
    }
    const initial = async(index, name) => {
        A_Z_style(index);
        if(name == '热门') {
            name = -1;
        }        let { data } = await axios.get(`/artist/list?type=${artist.type}&area=${artist.area}&initial=${name}`);
        singerData.value = data.artists;
        console.log(data)
    }


    const toSingerRoute = (id) => {
        router.push({name: 'singersong', query: {id}})
        console.log(id);
    }



</script>

<style lang="less" scoped>
    main{
        width: 980px;
        border: 1px solid #ccc;
        margin: 0 auto;
        display: flex;
    }
    article{
        width: 180px;
        border-right: 1px solid #ccc;
        flex-shrink: 0;
        ul{
            &:first-child{
                padding-top: 50px;
            }
            &:last-child{
                border-bottom: none;
            }
            border-bottom: 1px solid #ccc;
            padding: 20px;
            strong{
                display: block;
                margin-bottom: 10px;
            }
            li{
                list-style: square;
                color: #555;
                margin-left: 20px;
                font-size: 10px;
                line-height: 25px;
                cursor: pointer;
            }
            .active{
                color: red;
            }
        }
    }
    aside{
        flex-grow: 1;
        box-sizing: border-box;
        padding: 50px 40px;
        header{
            font-size: 23px; 
            border-bottom: 2.5px solid #c20c0c;
            line-height: 50px;
        }
        nav{
            display: flex;
            div{
                margin-top: 10px;
                flex-grow: 1;
                text-align: center;
                font-size: 13px;
                padding: 3px 0px;
                user-select: none;
                cursor: pointer;
                border-radius: 2px;
                &:hover{
                    text-decoration: underline;
                }
            }
            .active{
                color: white;
                background-color: #c20c0c;
                &:hover{
                    text-decoration: none;
                }
            }
        }
        ul{
            margin-top: 25px;
            display: grid;
            grid-template-columns: repeat(5, 1fr);
            gap: 30px 20px;
            li{
                cursor: pointer;
                height: 140px; 
                display: grid;
                grid-template-rows: 1fr 30px;
                img{
                    width: 100%;
                    height: 100%;
                }
                span{
                    font-size: 13px;
                    line-height: 30px;
                }
            }
        }
    }
</style>